﻿/********************************************************************************************
 * 文件名称:	Movie.cs
 * 设计人员:	严(yanxianghui@gmail.com)
 * 设计时间:	2009-03-16
 * 功能描述:	
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using Shanfree.SharpMovie.Model;
using Shanfree.SharpMovie.IDAL;
using Shanfree.SharpMovie.DALFactory;
using Shanfree.Framework.Utility;
using System.Collections;
using SIRC.Framework.Net;
using System.IO;

namespace Shanfree.SharpMovie.BLL
{
    /// <summary>
    /// 新闻操作
    /// </summary>
    public class Movie
    {
        private static readonly IMovie dal = DataAccess.CreateMovie();
        private static readonly IMovieNationRelate dalRela = DataAccess.CreateMovieNationRelate();

        /// <summary>
        /// 根据栏目获取新闻列表
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns></returns>
        public IList<MovieInfo> GetList(int pageSize, int pageIndex)
        {
            if (pageSize < 1 || pageIndex < 0)
            {
                throw new ArgumentNullException("pageSize or pageIndex ");
            }
            return dal.GetList(pageSize, pageIndex);
        }

        /// <summary>
        /// 获取新闻
        /// </summary>
        /// <param name="menuID">ID</param>
        /// <returns></returns>
        public MovieInfo GetByID(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }
            return dal.GetByID(id);
        }

        /// <summary>
        ///  imdb编号对应的电影是否存在数据库中
        /// </summary>
        /// <param name="imdbNo">imdb编号</param>
        /// <returns></returns>
        public bool Exist(string imdbNo)
        {
            if (string.IsNullOrEmpty(imdbNo))
            {
                throw new ArgumentNullException("imdbNo");
            }
            return dal.Exist(imdbNo);
        }

        /// <summary>
        ///  imdb编号对应的电影导入数据库中
        /// </summary>
        /// <param name="imdbNo">imdb编号</param>
        /// <param name="userID">用户主键</param>
        /// <returns>电影主键</returns>
        public string Import(string imdbNo, string userID)
        {
            // 从imdb网站导入
            EntityXpathInfo eInfo = GetMappingFromFile(imdbNo);
            return Import(eInfo, imdbNo, userID);
        }
        /// <summary>
        ///  imdb编号对应的电影导入数据库中,如果电影已经存在，返回电影主键
        /// </summary>
        /// <param name="imdbNo">imdb编号</param>
        /// <param name="eInfo">导入配置文件</param>
        /// <param name="userID">用户主键</param>
        /// <returns>电影主键</returns>
        public string Import(EntityXpathInfo eInfo, string imdbNo, string userID)
        {
            if (string.IsNullOrEmpty(imdbNo))
            {
                throw new ArgumentNullException("imdbNo");
            }
            // imdb编号引发的跳转，比如imdbA=imdbB，仍然是同一部影片
            MovieInfo mInfo = this.GetByIMDB_NO(imdbNo);
            if (mInfo != null && string.IsNullOrEmpty(mInfo.ID) == false)
            {
                return mInfo.ID;
            }

            eInfo.CustomerString = imdbNo;
            //http://www.imdb.com/title/tt0111161/
            string url = string.Format(eInfo.Source, eInfo.CustomerString);
            eInfo.URL = url;
            mInfo = new EntityInitialHander<MovieInfo>().GetEntityFromXpath(eInfo);
            return new Movie().Add(mInfo.IMDB_ID, mInfo.OriginalName, mInfo.EnglishName, mInfo.ChineseName, mInfo.HongkongName, mInfo.PostLink, mInfo.ProduceYear, mInfo.Director, mInfo.LeadingRule, mInfo.Introduce, mInfo.Awards, userID);
        }

        /// <summary>
        /// 添加电影
        /// </summary>
        /// <param name="IMDB_ID">IMDB编号</param>
        /// <param name="OriginalName">影片名称</param>
        /// <param name="EnglishName">英文名称</param>
        /// <param name="ChineseName">中文名称</param>
        /// <param name="HongkongName">台港澳名称</param>
        /// <param name="PostLink">海报链接</param>
        /// <param name="ProduceYear">上映年份</param>
        /// <param name="Director">导演</param>
        /// <param name="LeadingRule">主演</param>
        /// <param name="Introduce">简介</param>
        /// <param name="Awards">获得奖项</param>
        /// <param name="userID">发布用户</param>
        /// <returns>记录主键</returns>
        public string Add(string IMDB_ID, string originalName, string englishName,
                          string chineseName, string hongkongName, string postLink,
                          string produceYear, string director, string leadingRule,
                          string introduce, string awards, string userID)
        {
            // 是否可以把验证放到特性（Persistence）中实现，好处，坏处各是什么？
            if (string.IsNullOrEmpty(IMDB_ID))
            {
                throw new ArgumentNullException("IMDB编号不能为空。");
            }
            if (string.IsNullOrEmpty(originalName))
            {
                throw new ArgumentNullException("影片名称不能为空。");
            }
            if (string.IsNullOrEmpty(userID))
            {
                throw new ArgumentNullException("发布用户不能为空。");
            }
            // imdb编号引发的跳转，比如imdbA=imdbB，仍然是同一部影片
            MovieInfo mInfo = this.GetByIMDB_NO(IMDB_ID);
            if (mInfo != null && string.IsNullOrEmpty(mInfo.ID) == false)
            {
                return mInfo.ID;
            }
            // 影片不存在
            mInfo = new MovieInfo();
            mInfo.IMDB_ID = IMDB_ID;
            mInfo.OriginalName = originalName;
            mInfo.EnglishName = englishName;
            mInfo.ChineseName = chineseName;
            mInfo.HongkongName = hongkongName;
            mInfo.PostLink = postLink;
            mInfo.ProduceYear = produceYear;
            mInfo.Director = director;
            mInfo.LeadingRule = leadingRule;
            mInfo.Introduce = introduce;
            mInfo.Awards = awards;
            mInfo.UserID = userID;
            return dal.Add(mInfo);
        }

        /// <summary>
        /// 删除电影
        /// </summary>
        /// <param name="id">主键</param>
        public void Delete(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }
            MovieInfo aInfo = new MovieInfo();
            aInfo.ID = id;
            dal.Delete(aInfo);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">记录主键</param>
        /// <param name="IMDB_ID">IMDB编号</param>
        /// <param name="OriginalName">影片名称</param>
        /// <param name="EnglishName">英文名称</param>
        /// <param name="ChineseName">中文名称</param>
        /// <param name="HongkongName">台港澳名称</param>
        /// <param name="PostLink">海报链接</param>
        /// <param name="ProduceYear">上映年份</param>
        /// <param name="Director">导演</param>
        /// <param name="LeadingRule">主演</param>
        /// <param name="Introduce">简介</param>
        /// <param name="Awards">获得奖项</param>
        /// <param name="userID">发布用户</param>
        /// <
        public void Update(string id, string IMDB_ID, string originalName, string englishName,
                          string chineseName, string hongkongName, string postLink,
                          string produceYear, string director, string leadingRule,
                          string introduce, string awards, string userID)
        {
            if (string.IsNullOrEmpty(IMDB_ID))
            {
                throw new ArgumentNullException("IMDB编号不能为空。");
            }
            if (string.IsNullOrEmpty(originalName))
            {
                throw new ArgumentNullException("影片名称不能为空。");
            }
            if (string.IsNullOrEmpty(produceYear))
            {
                throw new ArgumentNullException("上映年份不能为空。");
            }
            if (string.IsNullOrEmpty(director))
            {
                throw new ArgumentNullException("导演信息不能为空。");
            }
            if (string.IsNullOrEmpty(leadingRule))
            {
                throw new ArgumentNullException("主演不能为空。");
            }
            if (string.IsNullOrEmpty(userID))
            {
                throw new ArgumentNullException("发布用户不能为空。");
            }
            MovieInfo mInfo = new MovieInfo();
            mInfo.ID = id;
            mInfo.IMDB_ID = IMDB_ID;
            mInfo.OriginalName = originalName;
            mInfo.EnglishName = englishName;
            mInfo.ChineseName = chineseName;
            mInfo.HongkongName = hongkongName;
            mInfo.PostLink = postLink;
            mInfo.ProduceYear = produceYear;
            mInfo.Director = director;
            mInfo.LeadingRule = leadingRule;
            mInfo.Introduce = introduce;
            mInfo.Awards = awards;
            mInfo.UserID = userID;
            dal.Update(mInfo);
        }


        /// <summary>
        /// 删除电影
        /// </summary>
        /// <param name="id">主键</param>
        public void AddNation(string movieID, string nationID)
        {
            if (string.IsNullOrEmpty(movieID))
            {
                throw new ArgumentNullException("movieID");
            }
            if (string.IsNullOrEmpty(nationID))
            {
                throw new ArgumentNullException("nationID");
            }
            MovieNationRelateInfo mInfo = new MovieNationRelateInfo();
            mInfo.NationID = nationID;
            mInfo.MovieID = movieID;
            dalRela.Add(mInfo);
        }

        /// <summary>
        /// 根据imdb编号获取影片信息
        /// 存在，从数据库加载
        /// 否则，网上获取
        /// </summary>
        /// <param name="imdbNo"></param>
        /// <returns></returns>
        public MovieInfo GetByIMDB_NO(string imdbNo)
        {
            if (string.IsNullOrEmpty(imdbNo))
            {
                throw new ArgumentNullException();
            }
            if (Exist(imdbNo))
            {
                return dal.GetByIMDB_NO(imdbNo);
            }
            else
            {
                EntityXpathInfo eInfo = GetMappingFromFile(imdbNo);
                MovieInfo mInfo = new MovieInfo();
                mInfo = new EntityInitialHander<MovieInfo>().GetEntityFromXpath(eInfo);
                return mInfo;
            }
        }

        /// <summary>
        /// 加载映射文档
        /// </summary>
        /// <returns></returns>
        public EntityXpathInfo GetMappingFromFile(string imdbNo)
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\EntityMapping.xml");
            EntityXpathInfo eInfo = SerializeHandler<EntityXpathInfo>.DeSerializeFromXMLFile(filePath);
            eInfo.CustomerString = imdbNo;
            //http://www.imdb.com/title/tt0111161/
            string url = string.Format(eInfo.Source, eInfo.CustomerString);
            eInfo.URL = url;
            return eInfo;
        }
    }
}
